home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-04-21 | 7.4 KB | 292 lines | [TEXT/MPS ] |
- // The C++ Booch Components (Version 2.1)
- // (C) Copyright 1990-1993 Grady Booch. All Rights Reserved.
- //
- // Restricted Rights Legend
- // Use, duplication, or disclosure is subject to restrictions as list forth
- // in subdivision (c)(1)(ii) of the Rights in Technical Data and Computer
- // Software clause at DFARS 252.227-7013.
- //
- // BCBTree.cpp
- //
- // This file contains the definitions for the binary tree.
-
- #include "BCExcept.h"
- #include "BCTree.h"
-
- template<class Item, class StorageManager>
- BC_TBinaryTree<Item, StorageManager>::BC_TBinaryTree()
- : fRep(0) {}
-
- template<class Item, class StorageManager>
- BC_TBinaryTree<Item, StorageManager>::
- BC_TBinaryTree(const BC_TBinaryTree<Item, StorageManager>& t)
- : fRep(t.fRep)
- {
- if (fRep)
- fRep->fCount++;
- }
-
- template<class Item, class StorageManager>
- BC_TBinaryTree<Item, StorageManager>::~BC_TBinaryTree()
- {
- Clear();
- }
-
- template<class Item, class StorageManager>
- BC_TBinaryTree<Item, StorageManager>& BC_TBinaryTree<Item, StorageManager>::
- operator=(const BC_TBinaryTree<Item, StorageManager>& t)
- {
- if (this == &t)
- return *this;
- Clear();
- fRep = t.fRep;
- if (fRep)
- fRep->fCount++;
- return *this;
- }
-
- template<class Item, class StorageManager>
- BC_Boolean BC_TBinaryTree<Item, StorageManager>::
- operator==(const BC_TBinaryTree<Item, StorageManager>& t) const
- {
- return (fRep == t.fRep);
- }
-
- template<class Item, class StorageManager>
- BC_Boolean BC_TBinaryTree<Item, StorageManager>::
- operator!=(const BC_TBinaryTree<Item, StorageManager>& t) const
- {
- return !operator==(t);
- }
-
- template<class Item, class StorageManager>
- void BC_TBinaryTree<Item, StorageManager>::Clear()
- {
- Purge(fRep);
- fRep = 0;
- }
-
- template<class Item, class StorageManager>
- void BC_TBinaryTree<Item, StorageManager>::Insert(const Item& i, BC_Child child)
- {
- BC_Assert((!fRep || (fRep->fParent == 0)),
- BC_XNotRoot("BC_TBinaryTree::Insert", BC_kNotRoot));
- if (child == BC_kLeft)
- fRep = new BC_TBinaryNode<Item, StorageManager>(i, 0, fRep, 0);
- else
- fRep = new BC_TBinaryNode<Item, StorageManager>(i, 0, 0, fRep);
- }
-
- template<class Item, class StorageManager>
- void BC_TBinaryTree<Item, StorageManager>::
- Append(const Item& i, BC_Child child, BC_Child after)
- {
- if (!fRep)
- fRep = new BC_TBinaryNode<Item, StorageManager>(i, 0, 0, 0);
- else {
- if (after == BC_kLeft) {
- if (child == BC_kLeft)
- fRep->fLeft =
- new BC_TBinaryNode<Item, StorageManager>(i, fRep, fRep->fLeft, 0);
- else
- fRep->fLeft =
- new BC_TBinaryNode<Item, StorageManager>(i, fRep, 0, fRep->fLeft);
- } else {
- if (child == BC_kLeft)
- fRep->fRight =
- new BC_TBinaryNode<Item, StorageManager>(i, fRep, fRep->fRight, 0);
- else
- fRep->fRight =
- new BC_TBinaryNode<Item, StorageManager>(i, fRep, 0, fRep->fRight);
- }
- }
- }
-
- template<class Item, class StorageManager>
- void BC_TBinaryTree<Item, StorageManager>::Remove(BC_Child child)
- {
- BC_Assert((fRep != 0), BC_XIsNull("BC_TBinaryTree::Remove", BC_kNull));
- if (child == BC_kLeft) {
- Purge(fRep->fLeft);
- fRep->fLeft = 0;
- } else {
- Purge(fRep->fRight);
- fRep->fRight = 0;
- }
- }
-
- template<class Item, class StorageManager>
- void BC_TBinaryTree<Item, StorageManager>::
- Share(BC_TBinaryTree<Item, StorageManager>& t, BC_Child child)
- {
- BC_TBinaryNode<Item, StorageManager>* ptr = t.fRep;
- BC_Assert((ptr != 0), BC_XIsNull("BC_TBinaryTree::Share", BC_kNull));
- if (child == BC_kLeft)
- ptr = t.fRep->fLeft;
- else
- ptr = t.fRep->fRight;
- Clear();
- fRep = ptr;
- fRep->fCount++;
- }
-
- template<class Item, class StorageManager>
- void BC_TBinaryTree<Item, StorageManager>::
- SwapChild(BC_TBinaryTree<Item, StorageManager>& t, BC_Child child)
- {
- BC_Assert((fRep != 0), BC_XIsNull("BC_TBinaryTree::SwapChild", BC_kNull));
- BC_Assert((!t.fRep || (t.fRep->fParent == 0)),
- BC_XNotRoot("BC_TBinaryTree::SwapChild", BC_kNotRoot));
- BC_TBinaryNode<Item, StorageManager>* curr;
- if (child == BC_kLeft) {
- curr = fRep->fLeft;
- fRep->fLeft = t.fRep;
- } else {
- curr = fRep->fRight;
- fRep->fRight = t.fRep;
- }
- if (t.fRep)
- t.fRep->fParent = fRep;
- t.fRep = curr;
- if (t.fRep)
- t.fRep->fParent = 0;
- }
-
- template<class Item, class StorageManager>
- void BC_TBinaryTree<Item, StorageManager>::Child(BC_Child child)
- {
- if (child == BC_kLeft)
- LeftChild();
- else
- RightChild();
- }
-
- template<class Item, class StorageManager>
- void BC_TBinaryTree<Item, StorageManager>::LeftChild()
- {
- BC_Assert((fRep != 0), BC_XIsNull("BC_TBinaryTree::LeftChild", BC_kNull));
- BC_TBinaryNode<Item, StorageManager>* curr = fRep;
- fRep = fRep->fLeft;
- if (curr->fCount > 1) {
- curr->fCount--;
- if (fRep)
- fRep->fCount++;
- } else {
- if (fRep)
- fRep->fParent = 0;
- if (curr->fRight)
- curr->fRight->fParent = 0;
- delete curr;
- }
- }
-
- template<class Item, class StorageManager>
- void BC_TBinaryTree<Item, StorageManager>::RightChild()
- {
- BC_Assert((fRep != 0), BC_XIsNull("BC_TBinaryTree::RightChild", BC_kNull));
- BC_TBinaryNode<Item, StorageManager>* curr = fRep;
- fRep = fRep->fRight;
- if (curr->fCount > 1) {
- curr->fCount--;
- if (fRep)
- fRep->fCount++;
- } else {
- if (fRep)
- fRep->fParent = 0;
- if (curr->fLeft)
- curr->fLeft->fParent = 0;
- delete curr;
- }
- }
-
- template<class Item, class StorageManager>
- void BC_TBinaryTree<Item, StorageManager>::Parent()
- {
- BC_Assert((fRep != 0), BC_XIsNull("BC_TBinaryTree::Parent", BC_kNull));
- if (!fRep->fParent)
- Clear();
- else {
- fRep->fCount--;
- fRep = fRep->fParent;
- if (fRep)
- fRep->fCount++;
- }
- }
-
- template<class Item, class StorageManager>
- void BC_TBinaryTree<Item, StorageManager>::SetItem(const Item& i)
- {
- BC_Assert((fRep != 0), BC_XIsNull("BC_TBinaryTree::SetItem", BC_kNull));
- fRep->fItem = i;
- }
-
- template<class Item, class StorageManager>
- BC_Boolean BC_TBinaryTree<Item, StorageManager>::HasChildren() const
- {
- return (!fRep || fRep->fLeft || fRep->fRight);
- }
-
- template<class Item, class StorageManager>
- BC_Boolean BC_TBinaryTree<Item, StorageManager>::IsNull() const
- {
- return (fRep == 0);
- }
-
- template<class Item, class StorageManager>
- BC_Boolean BC_TBinaryTree<Item, StorageManager>::IsShared() const
- {
- return (fRep) ? (fRep->fCount > 1) : 0;
- }
-
- template<class Item, class StorageManager>
- BC_Boolean BC_TBinaryTree<Item, StorageManager>::IsRoot() const
- {
- return (!fRep || (fRep->fParent == 0));
- }
-
- template<class Item, class StorageManager>
- const Item& BC_TBinaryTree<Item, StorageManager>::ItemAt() const
- {
- BC_Assert((fRep != 0), BC_XIsNull("BC_TBinaryTree::ItemAt", BC_kNull));
- return fRep->fItem;
- }
-
- template<class Item, class StorageManager>
- Item& BC_TBinaryTree<Item, StorageManager>::ItemAt()
- {
- BC_Assert((fRep != 0), BC_XIsNull("BC_TBinaryTree::ItemAt", BC_kNull));
- return fRep->fItem;
- }
-
- template<class Item, class StorageManager>
- void* BC_TBinaryTree<Item, StorageManager>::operator new(size_t s)
- {
- return StorageManager::Allocate(s);
- }
-
- template<class Item, class StorageManager>
- void BC_TBinaryTree<Item, StorageManager>::operator delete(void* p, size_t s)
- {
- StorageManager::Deallocate(p, s);
- }
-
- template<class Item, class StorageManager>
- void BC_TBinaryTree<Item, StorageManager>::
- Purge(BC_TBinaryNode<Item, StorageManager>*& curr)
- {
- if (curr) {
- if (curr->fCount > 1)
- curr->fCount--;
- else {
- Purge(curr->fLeft);
- if (curr->fLeft)
- curr->fLeft->fParent = 0;
- Purge(curr->fRight);
- if (curr->fRight)
- curr->fRight->fParent = 0;
- delete curr;
- curr = 0;
- }
- }
- }
-